<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 *
 * 创建时间：2021-11-03 14:48:29
 *
 * 项目：/levfm  -  $  - FormDataController.php
 *
 * 作者：{{AUTO GENERATE}}
 */

//此文件使用程序自动生成，下次生成时【不会】覆盖。
//在这里可以补充、完善你的程序逻辑

namespace modules\levfm\controllers;

use Lev;
use lev\base\Assetsv;
use lev\base\Controllerv;
use lev\base\Viewv;
use modules\levfm\table\form\FormData\FormData;
use modules\levfm\table\form\FormData\formValidations;
use modules\levfm\table\form\FormData\totalitemHelper;
use modules\levfm\table\LevfmDatas;
use modules\levfm\table\LevfmForms;

!defined('INLEV') && exit('Access Denied LEV');


//Adminv::checkAccess();
//Assetsv::registerSuperman();

class FormDataController extends Controllerv
{
    public static $renderFile = 'form-data/form_setup';

    /**
     * Renders the index view for the module
     * @see Modelv::inputsSetup()
     * @see Modelv::setupDesc()
     * @see Modelv::findOne()
     */
    public static function actionIndex($param = []) {
        ($param = $param ?: static::Form()) && Viewv::render(static::$renderFile, $param);
    }
    public static function Form($opid = null, $form = null) {
        $dosubmit = Lev::POSTv('dosubmit');

        $opid === null &&
        $opid = floatval(Lev::GETv('opid'));
        $opInfo = $opid >0 ? FormData::setopInfo($opid) : [];

        $form === null &&
        $form = floatval(Lev::GPv('form')) ?: ($opInfo ? $opInfo['formid'] : 0);

        $formInfo = FormData::formInfo($form);
        $formInfo['totalitem'] = Lev::getSettings($formInfo['totalitem']);
        $formInfo['settings']  = Lev::getSettings($formInfo['settings']);

        if ($errorMsg = formValidations::errorMessage($formInfo, $dosubmit)) {
            //isset($errorMsg['redirect']) ? parent::redirect($errorMsg['redirect']) :
            Lev::showMessages($errorMsg);
        }

        $day = Lev::stripTags(Lev::GETv('day'));
        if (is_numeric($day)) {
            $formInfo['_day'] = $day;
        }
        $totalitemBtn = totalitemHelper::getTotalItemDaysBtn($formInfo);

        FormData::$formInfo = FormData::setFormSettings($formInfo['settings'], $formInfo);

        $inputsSetup = FormData::inputsSetup();

        $formPre = 'datax';

        $route = parent::$route;

        if ($dosubmit) {
            Lev::showMessages(FormData::saveForm($formPre, $route, $form));
        }

        $setup = Lev::stripTags(Lev::GETv('setup'));

        $setupDesc = FormData::setupDesc();
        $inputs = isset($inputsSetup[$setup]) ? $inputsSetup[$setup] : ($inputsSetup ? reset($inputsSetup) : []);

        if (FormData::$opInfo) {
            FormData::$opInfo = FormData::setFormSettings(FormData::$opInfo['settings'], FormData::$opInfo);
            FormData::$opInfo+= LevfmDatas::getDatas(FormData::$opInfo['datas']);
            Lev::$app['title'] .= '编辑：'.FormData::$opInfo['id'];
        }else {
            Lev::$app['title'] .= '创建';
        }

        Lev::$app['metakeyword'].= $formInfo['name'];
        Lev::$app['metadesc']   .= $formInfo['name'].'。'.$formInfo['descs'];

        Assetsv::animateCss();
        Assetsv::ajaxFormJs();

        return [//所有form表单公用模板 - 如需修改请重新创建
            'opid'         => $opid,
            'inputs'       => $inputs,
            'commonInputs' => FormData::commonInputs(),
            'extInputs'    => FormData::extInputs(),
            'setupDesc'    => $setupDesc,
            'inputsValues' => FormData::$opInfo,
            'formPre'      => $formPre,
            'setup'        => $setup,
            'addurl'       => Lev::toReRoute([$route]),

            'formInfo'     => FormData::$formInfo,
            'totalitemBtn' => $totalitemBtn,

            'headerHtm'          => FormData::headerHtm(),
            'footerHtm'          => FormData::footerHtm(),
            'footerFormInnerHtm' => FormData::footerFormInnerHtm(),
        ];
    }
}